<script>
	import {
		userApi
	} from '@/utils/api.js';
	export default {
		onLaunch: function(op) {
			if (op.referrer_id) {
				uni.setStorageSync('referrer_id', op.referrer_id);
			} else {
				uni.setStorageSync('referrer_id', 0);
			}

			console.log('App Launch')
			let token = uni.getStorageSync('token');
			if (token) {
				return; //有token就无需自动登录。
			}
			uni.login({
				provider: 'weixin',
				success(res) {
					if (res.code) {
						userApi.login({
							code: res.code,
							referrer_id: uni.getStorageSync('referrer_id')
						}).then((loginRes) => {
							// 保存token
							uni.setStorageSync('token', loginRes.data.token);
							// 获取并保存用户信息
							userApi.getUserInfo().then((userInfo) => {
								uni.setStorageSync('userInfo', userInfo.data);
							});
						});
					}
				}
			});
		},
		onShow: function() {
			console.log('App Show')
		},
		onHide: function() {
			console.log('App Hide')
		},
		methods: {

		}
	}
</script>

<style lang="scss">
	/*每个页面公共css */
	@import '@/uni_modules/uni-scss/index.scss';
	/* #ifndef APP-NVUE */
	@import '@/static/customicons.css';

	// 设置整个项目的背景色
	page {
		background-color: #f5f5f5;
	}

	/* #endif */
	.example-info {
		font-size: 14px;
		color: #333;
		padding: 10px;
	}
</style>